﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using NTQOnlineExam.Services.SearchCriteria;

namespace NTQOnlineExamFrontend.Helpers
{
    public static class Utility
    {
        public static IList<SearchSortItem> GetSortItems(HttpRequestBase request)
        {
            if (request == null) return new List<SearchSortItem>();
            var requestInformation = request.HttpMethod.ToLower().Equals("post") ? request.Form : request.QueryString;
            var sortLength = 0;
            if (requestInformation["sortLength"] != null) int.TryParse(requestInformation["sortLength"].ToString(), out sortLength);
            var sortItems = new List<SearchSortItem>();
            if (sortLength == 0) return sortItems;
            for (var i = 1; i <= sortLength; i++)
            {
                var sortExpression = requestInformation["sortExpression" + i];
                var sortOrder = "asc".Equals(requestInformation["sortOrder" + i]) ? SortOrder.Ascending : ("desc".Equals(requestInformation["sortOrder" + i]) ? SortOrder.Descending : SortOrder.Unspecified);
                if (sortOrder != SortOrder.Unspecified) sortItems.Add(new SearchSortItem(sortExpression, sortOrder));
            }
            return sortItems;
        }
    }
}